package cn.com.callback

import cn.com.elite.EliteBean
import cn.com.kh.KHDBConfig
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.springframework.jdbc.core.JdbcTemplate

class autoDxImportCust implements CallBackInvoke {
    private static final Log log = LogFactory.getLog(autoDxImportCust.class);

    @Override
    void doCallBack(EliteBean eliteBean, String s, Map map, String s1) {

        if (KHDBConfig.getParam(eliteBean,"JYDRS")=="OPEN") {
            JdbcTemplate jt = new JdbcTemplate(eliteBean.getDataSource());
            //将锁开启
            String updatesql = '''update sysc_parameter set PARAMETERVALUE='CLOSE' where PARAMETER_ID='JYDRS' ''';
            jt.update(updatesql);

            //查询是否有符合下发条件的批次，包括活动任务名单、常规任务名单、特定任务名单
            List accountList = eliteBean.chaxun_sql("select h.rec_guid,h.batchid,c.level,h.starttime from hst_activity_batch h left join c_codetables c on h.task_type=c.code_id left join hst_activity ac on h.tab_activity_id=ac.tab_activity_id " +
                    "where use_type='电销外呼类型' and h.wholestate='0'  and h.state='1' and ((level='1' or level='2' and h.starttime<=now()) or (level='3' and h.starttime<=now() and now()>=ac.tab_activity_starttime  and now()<=ac.tab_activity_endtime)) " +
                    "order by h.starttime", "dataSource");

            //循环将每个批次的数据新增至account_information表处理
            for (int i = 0; i < accountList.size(); i++) {
                long startTime = System.currentTimeMillis();
                Map accountMap = accountList.get(i);
                String batchid = accountMap.get("batchid");//处理任务主键

                //批次数据添加至account_information表中，后续由autoDxCustIssued.groovy执行定时下发
                String insertTask = "INSERT INTO account_information(rec_id, customerid, task_type, task_guid, createtime, deal_flag,source,batch_id) select uuid(),hst_no,task_type,uuid(),now(),0,1,'" + batchid + "'   from hst_activity_batch_list  where batchid='" + batchid + "' and result_code='0'";
                int row = eliteBean.update(insertTask, "dataSource");
                log.debug("开始执行批次:" + batchid + "|" + row);

                //执行成功后将批次表hst_activity_batch中
                if (row > 0) {
                    String updateRow = "update hst_activity_batch set wholestate='1' where batchid='" + batchid + "'";
                    int rowNum = eliteBean.update(updateRow, "dataSource");
                    log.debug("批次" + batchid + "执行成功" + rowNum);
                }
                long endTime = System.currentTimeMillis();
                log.debug("批次处理完成:" + (endTime - startTime));

            }


            /**
             * @Title:
             * @Description:通用外呼规则名单
             * @Param:
             * @return: void
             * @Author: TaoHao
             * @Date: 2024/10/18
             */
            //查询是否有符合下发条件的批次

            String accSql = '''
                              SELECT
                              h.rec_guid,
                              h.batchid,
                              h.starttime,
                              h.tab_activity_id,
                              ac.activity_type,
                              h.table_name,
                              h.task_type
                              FROM
                              pa_activity_batch h
                              LEFT JOIN pa_activity ac ON h.tab_activity_id = ac.tab_activity_id 
                              WHERE
                              1 = 1 
                              AND h.wholestate = '0' 
                              AND h.state = '1' 
                              AND  h.starttime <= now() 
                              AND now()>= ac.tab_activity_starttime AND now()<= ac.tab_activity_endtime 
                              ORDER BY
                              h.starttime
                                            ''';

            List<Map<String, Object>> BatchList = jt.queryForList(accSql);

            //循环将每个批次的数据新增至account_information表处理
            for (int i = 0; i < BatchList.size(); i++) {
                long startTime = System.currentTimeMillis();
                Map accountMap = BatchList.get(i);
                String batchid = accountMap.get("batchid");//处理任务主键
                String tab_activity_id = accountMap.get("tab_activity_id");
                String activity_type = accountMap.get("activity_type");//1.是自动化扫表 2.手动导入
                String table_name = accountMap.get("table_name");//自动化数据表名
                String task_type = accountMap.get("task_type");


                String checksql = ''' select rule_name from Hst_Rule_Issue where rule_name=? ''';
                List<Map<String, Object>> checksqlList = jt.queryForList(checksql, tab_activity_id);
                if (checksqlList.size() > 0) {
                    int row = 0;
                    if (activity_type.equals("2")) {
                        log.debug("扫描到是【导入】类型的批次:" + batchid);
                        //批次数据添加至pa_account_information表中，后续由autoCurrencyRuleIssued.groovy执行定时下发
                        String insertTask = "INSERT INTO pa_account_information(rec_id, customerid, task_type, task_guid, createtime, deal_flag,source,batch_id) select rec_id,hst_no,task_type,uuid(),now(),0,1,'" + batchid + "'   from pa_activity_batch_list  where batchid='" + batchid + "' and result_code='0'";
                        row = eliteBean.update(insertTask, "dataSource");
                        log.debug("开始执行批次:" + batchid + "|" + row);
                        if (row > 0) {
                            //执行成功后将批次表pa_activity_batch中
                            String updateRow = "update pa_activity_batch set wholestate='1' where batchid='" + batchid + "'";
                            int rowNum = eliteBean.update(updateRow, "dataSource");
                            log.debug("批次" + batchid + "执行成功" + rowNum);
                        }
                    } else if (activity_type.equals("1")) {
                        log.debug("扫描到是【自动】类型的批次:" + batchid + "数据表名:" + table_name);
                        String Insert = "INSERT INTO pa_activity_batch_list(rec_id,batchid,hst_no,task_type,result_code)select dws.rec_guid,'" + batchid + "',dws.investorid,'" + tab_activity_id + "',0 from dws." + table_name + "  dws  where Not EXISTS  (select  pa.rec_id from pa_activity_batch_list pa  where pa.rec_id=dws.rec_guid and pa.batchid='" + batchid + "' ) ";
                        log.debug("Insert:" + Insert);
                        eliteBean.update(Insert, "dataSource");
                        //批次数据添加至pa_account_information表中，后续由autoCurrencyRuleIssued.groovy执行定时下发
                        String insertTask = "INSERT INTO pa_account_information(rec_id, customerid, task_type, task_guid, createtime, deal_flag,source,batch_id) select rec_id,hst_no,task_type,uuid(),now(),0,1,'" + batchid + "'   from pa_activity_batch_list  where batchid='" + batchid + "' and result_code='0'";
                        row = eliteBean.update(insertTask, "dataSource");
                    }

                    log.debug("本次新增任务行数:" + row);

                    long endTime = System.currentTimeMillis();
                    log.debug("批次处理完成:" + (endTime - startTime));

                } else {
                    log.debug("批次" + batchid + "未配置规则");

                }


            }

            String updatesql2 = '''update sysc_parameter set PARAMETERVALUE='OPEN' where PARAMETER_ID='JYDRS' ''';
            jt.update(updatesql2);

        }

    }

}